import axios from 'axios'
import router from '../router/index'
export function request(option){
    //创建axios实例
    const instance = axios.create({
        baseURL:'/api',
        timeout:5000,
        headers:{
            'Content-Type':'application/x-www-form-urlencoded;charset=UTF-8'
        }
    });
    // axios请求拦截器
    instance.interceptors.request.use(config=>{
        console.log(config)
      //如果存在token,请求携带这个token,后端有拦截器去验证token是否正确
      let tokenStr = window.sessionStorage.getItem("tokenStr");
      if(tokenStr){
        config.headers['Authorization'] = tokenStr;
      }
        return config;
    },err => {
        console.log(err)
    });
    instance.interceptors.response.use(res=>{
        console.log(res)
        return res.data;
    },err => {
      //权限不足跳转到登录页
      console.log(err)
      //没有相关权限
      /*if(err.response.code && err.response.code == '401'){
        // Cookies.remove(TOKEN_KEY)
        router.replace('/');
      }*/
    });
    //返回的就是个Promise对象
    return instance(option);
}
